import 'package:flutter/material.dart';

import 'listData.dart';

class DynamicListView1 extends StatelessWidget {
  const DynamicListView1({super.key});

  //自定义方法
  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var i = 0; i < 20; i++) {
      list.add(ListTile(
        title: Text("我是一个列表---$i"),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: ListView(
        children: _initListData(),
      )),
    );
  }
}

class DynamicListView2 extends StatelessWidget {
  const DynamicListView2({super.key});
  //自定义方法
  List<Widget> _initListData() {
    return listData.map((value) {
      //// 将listData 数组 所有元素封装 并返回数组
      return ListTile(
        leading: Image.network("${value["imageUrl"]}"),
        title: Text("${value["title"]}"),
        subtitle: Text("${value["author"]}"),
      );
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: ListView(
        children: _initListData(),
      )),
    );
  }
}

class DynamicListView3 extends StatelessWidget {
  const DynamicListView3({super.key});
/*  //自定义方法
  List<Widget> _initListData() {
    return listData.map((value) {
      //// 将listData 数组 所有元素封装 并返回数组
      return ListTile(
        leading: Image.network("${value["imageUrl"]}"),
        title: Text("${value["title"]}"),
        subtitle: Text("${value["author"]}"),
      );
    }).toList();
  }*/

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(),
          body: ListView.builder(
              itemCount: listData.length,
              itemBuilder: (context, index) {
                return ListTile(
                  leading: Image.network(listData[index]["imageUrl"]),
                  title: Text(listData[index]["title"]),
                  subtitle: Text(listData[index]["author"]),
                );
              })),
    );
  }
}
